package com.yanqu.road.dao.impl.union.parergon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.parergon.UnionParergonPlayer;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionParergonPlayerDaoImpl extends TempDataObjectDao<UnionParergonPlayer> {

    public Map<Long, UnionParergonPlayer> getUnionParergonPlayerMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UnionParergonPlayer> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_union_parergon_player";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionParergonPlayer data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UnionParergonPlayer data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_union_parergon_player (userId, operateTimes, lastRestoreTime, bargainTime, bargainUnionUId, todayTimes, totalTimes, historyTimes,dayUnionUId, dayRestoreTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getOperateTimes());
            call.setLong(i++, data.getLastRestoreTime());
            call.setLong(i++, data.getBargainTime());
            call.setString(i++, data.getBargainUnionUId());
            call.setInt(i++, data.getTodayTimes());
            call.setInt(i++, data.getTotalTimes());
            call.setInt(i++, data.getHistoryTimes());
            call.setString(i++, data.getDayUnionUId());
            call.setLong(i++, data.getDayRestoreTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionParergonPlayer data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_union_parergon_player SET operateTimes=?, lastRestoreTime=?, bargainTime=?, bargainUnionUId=?, todayTimes=?, totalTimes=?, historyTimes=?,dayUnionUId=?, dayRestoreTime=? WHERE userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getOperateTimes());
            call.setLong(i++, data.getLastRestoreTime());
            call.setLong(i++, data.getBargainTime());
            call.setString(i++, data.getBargainUnionUId());
            call.setInt(i++, data.getTodayTimes());
            call.setInt(i++, data.getTotalTimes());
            call.setInt(i++, data.getHistoryTimes());
            call.setString(i++, data.getDayUnionUId());
            call.setLong(i++, data.getDayRestoreTime());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UnionParergonPlayer parseData(ResultSet rs) throws SQLException {
        UnionParergonPlayer data = new UnionParergonPlayer();
        data.setUserId(rs.getLong("userId"));
        data.setOperateTimes(rs.getInt("operateTimes"));
        data.setLastRestoreTime(rs.getLong("lastRestoreTime"));
        data.setBargainTime(rs.getLong("bargainTime"));
        data.setBargainUnionUId(rs.getString("bargainUnionUId"));
        data.setTodayTimes(rs.getInt("todayTimes"));
        data.setTotalTimes(rs.getInt("totalTimes"));
        data.setHistoryTimes(rs.getInt("historyTimes"));
        data.setDayUnionUId(rs.getString("dayUnionUId"));
        data.setDayRestoreTime(rs.getLong("dayRestoreTime"));
        return data;
    }
}
